<template>
    <div class="login-wapper">
        <div class="gt-wapper" :style="{fontSize:size}" v-if="visible">
            <div class="gt-content" :style="{width:width,marginTop:top}">
                <div class="gt-header">手机登录<i class="iconfont gt-header-close" @click="close">&#xe615;</i></div>
                <div class="gt-body">
                    <ul>
                        <li class="gt-body-item">
                            <input class="gt-body-phone" type="number" name="phone" placeholder="请输入手机号" v-model="phone">
                        </li>
                        <li class="gt-body-item flex">
                            <div class="flex-1">
                                <input type="number" name="code" placeholder="请输入验证码" v-model="code">
                            </div>
                            <div class="">
                                <p class="gt-body-sendbtn"  @click="getCode"
                                :style="{color:isSendActive?color:'#333',borderColor:isSendActive?color:'#e5e5e5'}">{{sendCodeTxt}}</p>
                            </div>
                        </li>
                    </ul>
                </div>
                <div class="gt-footer">
                    <p class="gt-btn" :style="{backgroundColor:color,opacity:isActive?'':0.5}" @click="submit">登录</p>
                </div>
                <!-- 错误提示语 -->
                <transition name="fade" >
                    <div class="gt-error" v-if="errorShow">{{errorTxt}}</div>
                </transition>
            </div>
        </div>
        
    </div>
</template>
<script>
import common from '../../../assets/js/common'
import '../../../assets/font/iconfont.css'
const md5 = require('md5')
export default {
    name:'gtLogin',
    props:{
        top:{
            type: String,
            default: '15vh'
        },
        width:{
            type: String,
            default: '80%'
        },
        size:{
            type: String,
            default: '16px'
        },
        color:{
            type: String,
            default: '#ef3b79'
        },
        visible:{
            type: Boolean,
            default: false
        },
        domain:{
            type: String,
            default: 'http://192.168.2.254:8080'
        },
        busId:{
            type: [String,Number],
            default: 36
        }
    },
    data(){
        return{
            sendCodeTxt:'获取验证码',
            errorTxt:'请输入正确的手机号',
            isSendActive:true,
            errorShow:false,
            timeleft: 60,
            phone:'',
            code:'',
            getCodeTime:''
        }
    },
    computed:{
        isActive(){
            return common.checkPhone(this.phone) && this.code.length == 6
        },
    },
    watch:{
        errorShow(){
            if(this.errorShow){
                setTimeout(_=>{
                    this.errorShow = false
                },1500)
            }
        }
    },
    methods:{
        /**
         * 清除定时器  回复验证码状态
         */
        cleartime(){
            let _this = this
            _this.isSendActive = true;
            _this.timeleft = 60;
            _this.sendCodeTxt = '重新获取';
            clearTimeout(_this.getCodeTime);
        },
        /**
         * 获取验证码
         */
        getCode(){
            let _this = this
            let json={};
            if(!common.checkPhone(this.phone)){
                this.errorShow = true
                _this.errorTxt = '请输入正确的手机号'
                return
            }
            json["busId"]= _this.busId;
            json["phone"]= _this.phone;
            json["country"] = 86;
            console.log('json===',json)
            if(!_this.isSendActive) return
            console.log(11111)
            common.ajax({
                'url': _this.domain + '/phoneLogin/phoneLoginController/memberSendMsg',
                'type': 'POST',
                'data':{
                    "json":json
                },
                'success':function (data){
                    console.log("vcode",data);
                },
                'warning':function(data){
                    _this.cleartime()
                    _this.errorShow = true
                    _this.errorTxt = '短信验证码已发送'
                },
                'error':function(data){
                    _this.errorShow = true
                    _this.errorTxt = data.msg
                    _this.cleartime()
                }
            })
            _this.getCodeTime=setInterval(function(){
                _this.isSendActive = false
                _this.timeleft--;
                _this.sendCodeTxt = _this.timeleft + 's';
                if(_this.timeleft==0){
                   _this.cleartime()
                }
            },1000);
        },
        /**
         * 登录
         */
        submit(){
            let _this = this
            let json={};
            let random = Math.floor(Math.random()*1000)
            if(!common.checkPhone(this.phone)){
                this.errorShow = true
                _this.errorTxt = '请输入正确的手机号'
                return
            }
            if(_this.code.length != 6){
                this.errorShow = true
                _this.errorTxt = '请输入正确的验证码'
                return
            }
            if(_this.isActive){
                json["busId"]= _this.busId;
                json["phone"]= _this.phone;
                json["loginTokenCode"]= _this.code;  /**验证码 */
                json["code"]= random;   /**随机数 */
                json["codeStr"]= md5( _this.code + '_' + _this.busId + '_' + random);   /**加密规则 */
                console.log(json,'json')
                common.ajax({
                    'url': _this.domain + '/phoneLogin/phoneLoginController/registerOrLogin',
                    'type': 'POST',
                    'data':{
                        "json":json
                    },
                    'success':function (data){
                        console.log("成功",data);
                        _this.$emit('update:visible', false);
                        _this.$emit('loginSuccess',data)
                    },
                    'warning':function(data){
                        _this.errorShow = true
                        _this.errorTxt = data.msg
                        _this.cleartime()
                    },
                    'error':function(data){
                        _this.errorShow = true
                        _this.errorTxt = data.msg
                        _this.cleartime()
                    }
                })
            }
        },
        /**
         * 关闭遮罩层
         * */
        close(){
            this.$emit('update:visible', false);
        }
    }
}
</script>

<style>
*{
    margin: 0;
    padding: 0;
    list-style: none;
    -webkit-tap-highlight-color: rgba(255, 255, 255, 0);
    -webkit-appearance: none; 
    outline: none;
    border: none;
    -webkit-tap-highlight-color: transparent !important; 
    touch-action: manipulation;
}
.iconfont{
	font-family:"iconfont"!important;
	font-size:16px;
	font-style:normal;
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;
} 
.flex{ display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex; }
.flex-1{ -webkit-box-flex:1;-webkit-flex:1;-ms-flex:1;flex:1; }
.gt-wapper{
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow: auto;
    margin: 0 auto;
    z-index: 9988;
    background-color: rgba(0, 0, 0, 0.5);
}
.login-wapper input[type='number']{
    font-size: 0.875em
}
.login-wapper input[type='number']::-webkit-input-placeholder,
.login-wapper input[type='number']::-moz-placeholder,
.login-wapper input[type='number']::-ms-input-placeholder{
    color: #c2c2c2;
}
.login-wapper .gt-content{
    margin: 0 auto;
    background-color: #fff;
    border-radius: 0.3em;
    font-size: inherit;
    padding: 1.40625em 0.75em ;
    position: relative
}
.login-wapper .gt-header{
    font-size: 1em;
    color: #333333;
    text-align: center;
}
.login-wapper .gt-header-close{
    position: absolute;
    top: 1.40625em;
    right: 0.75em;
    color: #d5d5d5
}
.login-wapper .gt-body{
    padding: 1.875em 0
}
.login-wapper .gt-body-item{
    border-bottom: 1px solid #e2e2e2;
    padding: 0.5em 0;
    position: relative;
    text-align: left
}
.login-wapper .gt-body-sendbtn{
    font-size: 0.75em;
	border-radius: 27px;
    border-width: 1px;
    border-style: solid;
    padding: 0.5em;
    min-width: 4.6875em;
    text-align: center;
    white-space: nowrap;
    box-sizing: border-box
}
.login-wapper input[name='code']{
    margin-top: 0.8em;
    width: 100%
}
.login-wapper .gt-btn{
  position: relative;
  display: block;
  margin-left: auto;
  margin-right: auto;
  box-sizing: border-box;
  text-align: center;
  text-decoration: none;
  color: #FFFFFF;
  line-height: 2.25;
  border-radius: 1.5em;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  overflow: hidden;
  font-size: 1em
}
/*错误提示*/
.login-wapper .gt-error{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    background: rgba(0, 0, 0, 0.75);
    color: #fff;
    font-size: 1em;
    border-radius: 0.3125em;
    padding: 0.9375em 1em;
    min-width: 11.25em;
    text-align: center;
}
/*错误提示过渡动画*/
.fade-enter-active, .fade-leave-active {
  transition: opacity 1.5s;
}
.fade-enter, .fade-leave-to,.fade-leave-active{
  opacity: 0;
}
</style>
